package com.ruoyi.machine.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;

import com.ruoyi.machine.common.Result;
import com.ruoyi.machine.domain.Dto.EExpressBoxUsageRecordsDto;
import com.ruoyi.machine.domain.Dto.EExpressBoxesDto;
import com.ruoyi.machine.domain.Dto.EExpressMachinesDto;
import com.ruoyi.machine.domain.EExpressBoxUsageRecords;
import com.ruoyi.machine.domain.EExpressBoxes;
import com.ruoyi.machine.domain.UseCountMap;
import com.ruoyi.machine.domain.Vo.EExpressBoxUsageRecordsVo;
import com.ruoyi.machine.domain.Vo.EExpressBoxesVo;
import com.ruoyi.machine.domain.Vo.EExpressMachinesVo;
import com.ruoyi.machine.mapper.EExpressBoxUsageRecordsMapper;
import com.ruoyi.machine.mapper.EExpressBoxesMapper;
import com.ruoyi.machine.service.EExpressBoxesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.machine.domain.EExpressMachines;
import com.ruoyi.machine.service.IEExpressMachinesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 柜机管理Controller
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/machine")
public class EExpressMachinesController extends BaseController
{
    @Autowired
    private IEExpressMachinesService eExpressMachinesService;
    @Autowired
    private EExpressBoxesMapper boxesMapper;

    @Autowired
    private EExpressBoxUsageRecordsMapper expressBoxUsageRecordsMapper;


    /**
     * 查询柜机管理列表
     */
    @RequiresPermissions("machine:machine:list")
    @GetMapping("/list")
    public TableDataInfo list(EExpressMachines eExpressMachines)
    {
        startPage();
        List<EExpressMachines> list = eExpressMachinesService.selectEExpressMachinesList(eExpressMachines);
        return getDataTable(list);
    }



//    @RequiresPermissions("machine:machine:list")
//    @GetMapping("/list")
//    public TableDataInfo list(EExpressMachinesVo vo)
//    {
//        startPage();
//        List<EExpressMachines> list = eExpressMachinesService.list(vo);
//        return getDataTable(list);
//    }
    @Autowired
    private EExpressBoxesService expressBoxesService;
@GetMapping("/findBoxRecordList")
public R findBoxRecordList(EExpressBoxUsageRecordsVo vo)
{
    startPage();
    List<EExpressBoxUsageRecordsDto> list = expressBoxesService.list(vo);
    Long count =Long.valueOf(expressBoxesService.count(vo));
    return R.ok(count, list.toString());
}

    /**
     * 导出柜机管理列表
     */
    @RequiresPermissions("machine:machine:export")

    @PostMapping("/export")
    public void export(HttpServletResponse response, EExpressMachines eExpressMachines)
    {
        List<EExpressMachines> list = eExpressMachinesService.selectEExpressMachinesList(eExpressMachines);
        ExcelUtil<EExpressMachines> util = new ExcelUtil<EExpressMachines>(EExpressMachines.class);
        util.exportExcel(response, list, "柜机管理数据");
    }

    @PostMapping("/updateStatus")
    public R updateStatus(@RequestBody EExpressMachines machines){
        eExpressMachinesService.updateStatus(machines);
        return R.ok();
    }

    /**
     * 获取柜机管理详细信息
     */
    @RequiresPermissions("machine:machine:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eExpressMachinesService.selectEExpressMachinesById(id));
    }

    /**
     * 新增柜机管理
     */
    @RequiresPermissions("machine:machine:add")

    @PostMapping
    public AjaxResult add(@RequestBody EExpressMachines eExpressMachines)
    {

        return toAjax(eExpressMachinesService.insertEExpressMachines(eExpressMachines));
    }



    /**
     * 修改柜机管理
     */
    @RequiresPermissions("machine:machine:edit")

    @PutMapping
    public AjaxResult edit(@RequestBody EExpressMachines eExpressMachines)
    {
        return toAjax(eExpressMachinesService.updateEExpressMachines(eExpressMachines));
    }
    /**
     * 删除柜机管理
     */
    @RequiresPermissions("machine:machine:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eExpressMachinesService.deleteEExpressMachinesByIds(ids));
    }

@GetMapping("/findBoxById/{id}")
public AjaxResult findBoxById(@PathVariable("id") Integer id){
    EExpressBoxesDto dto=expressBoxesService.findBoxById(id);
    return success(dto);
}
    @GetMapping("/findMachineById/{id}")
    public AjaxResult findMachineById(@PathVariable Long id){
        EExpressMachinesDto dto= eExpressMachinesService.findMachineById(id);
        Integer a=0;
        List<EExpressBoxes> list=boxesMapper.findBoxesById(dto.getId());
        for (EExpressBoxes boxes : list) {
            if (boxes.getIsStatus()==1){
                a++;
            }
        }
        if (a==0){
            return success(dto);
        }else {
            dto.setUseCount(a);
            return success(dto);
        }
    }

    @GetMapping("/findMachineByIdqq/{id}")
    public AjaxResult findMachineByIdqq(@PathVariable Long id){
        return success(eExpressMachinesService.findMachineByIdqq(id));
//        return eExpressMachinesService.findMachineById(id);
    }
//箱格条件展示
@RequestMapping("/getBoxList")
public AjaxResult getBoxList(@RequestBody EExpressBoxesVo vo){
    List<EExpressBoxesDto> list = boxesMapper.findBoxList(vo);
    ArrayList<Integer> ids = new ArrayList<>();
    for (EExpressBoxesDto dto : list) {
        ids.add(Math.toIntExact(dto.getId()));
    }
    if (ids != null && !ids.isEmpty()){
        List<UseCountMap> mapList = expressBoxUsageRecordsMapper.selectBoxUsageRecords(ids);
        List<UseCountMap> mapList1 = expressBoxUsageRecordsMapper.findOpenDateTime(ids);
        for (EExpressBoxesDto dto : list) {
            for (UseCountMap map : mapList) {
                if (map.getId().equals(dto.getId())){
                    dto.setOpenCount(map.getUseCount());
                }
            }
            for (UseCountMap map : mapList1) {
                if (map.getId().equals(dto.getId())){
                    dto.setOpenDateTime(map.getOpenDateTime());
                }
            }
        }
    }
    return success(list);
}
//批量添加
@PostMapping("/save")
public AjaxResult save(@RequestBody List<EExpressMachines> list){
    eExpressMachinesService.save(list);
    return success();
}


    @PostMapping("/updateBox")
    public AjaxResult updateBox(@RequestBody EExpressBoxes boxes){
        expressBoxesService.updateBox(boxes);
        return success();
    }
}
